package com.just521.xinshoutong.dialog;

import com.just521.xinshoutong.R;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;


public class CustomDlg extends Dialog
{
	protected 	Activity 	m_context;
	protected	int			m_iPosX;
	protected	int			m_iPosY;
	ListView msinglechoiceLayout;
	private Window window=null;
	OnClickListener msingleChoiceListener;
	View mContentView;
	
	public	CustomDlg( Activity context,int style )
	{
		super(context,style);
		m_context 	=	context;
		
	}
	public void setList(String[] items){
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		msinglechoiceLayout = new ListView(m_context);
		addContentView(msinglechoiceLayout, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
		msinglechoiceLayout.setCacheColorHint(Color.argb(0, 0, 0, 0));
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(m_context,R.layout.simple_list_item,items);
		
		msinglechoiceLayout.setAdapter(adapter);
		Drawable  draw = m_context.getResources().getDrawable(R.drawable.frame_img); 
		msinglechoiceLayout.setBackgroundDrawable(draw);
		draw = m_context.getResources().getDrawable(R.drawable.table_line); 
		msinglechoiceLayout.setDivider(draw);
		msinglechoiceLayout.setVerticalScrollBarEnabled(false);
	}
	public void setSingleChoice(String[] items,int select,OnClickListener listener){
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		
		msinglechoiceLayout = new ListView(m_context);
		
		msinglechoiceLayout.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		addContentView(msinglechoiceLayout, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
		msinglechoiceLayout.setCacheColorHint(Color.argb(0, 0, 0, 0));
		msinglechoiceLayout.setVerticalScrollBarEnabled(false);
		
		msingleChoiceListener = listener;

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(m_context,R.layout.simple_list_item_single_choice,items);
		
		msinglechoiceLayout.setAdapter(adapter);

		msinglechoiceLayout.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id){
				msingleChoiceListener.onClick(CustomDlg.this, position);
			}
			
		});
		
		msinglechoiceLayout.setItemChecked(0, true);
		Drawable  draw = m_context.getResources().getDrawable(R.drawable.frame_img); 
		msinglechoiceLayout.setBackgroundDrawable(draw);
		draw = m_context.getResources().getDrawable(R.drawable.table_line); 
		msinglechoiceLayout.setDivider(draw);
		
		setProperty();
		
	}
	public void setDisplayView(View view){
		setContentView(view);//设置对话框的布局
		
		setProperty();
	}
	
	public void setDisplayView(String title,String msg,String LBtn,String RBtn){
		LayoutInflater factory = LayoutInflater.from(m_context);
		final View v = factory.inflate(R.layout.dialog_lucky_common, null);
		mContentView=v;
		TextView tvtitle=(TextView)v.findViewById(R.id.dialog_title);
		if(title!=null){
			tvtitle.setText(title);
		}else{
			tvtitle.setText("提醒");
		}
		TextView tvmsg=(TextView)v.findViewById(R.id.dialog_msg);
		if(msg!=null){
			tvmsg.setText(msg);
		}else{
			tvmsg.setVisibility(View.GONE);
		}
		Button left = (Button)v.findViewById(R.id.dialog_btn_Left);
		if(LBtn!=null){
			left.setText(LBtn);
		}else{
			left.setVisibility(View.GONE);
		}
		Button right = (Button)v.findViewById(R.id.dialog_btn_Right);
		if(RBtn!=null){
			right.setText(RBtn);
		}else{
			right.setVisibility(View.GONE);
		}
		setContentView(v);//设置对话框的布局
		
		
		setProperty();
	}
	public void setDisplayWithView(int resource,String title,String msg,String LBtn,String RBtn){
		LayoutInflater factory = LayoutInflater.from(m_context);
		final View v = factory.inflate(resource, null);
		mContentView=v;
		TextView tvtitle=(TextView)v.findViewById(R.id.dialog_title);
		if(title!=null){
			tvtitle.setText(title);
		}else{
			tvtitle.setVisibility(View.GONE);
		}
		TextView tvmsg=(TextView)v.findViewById(R.id.dialog_msg);
		if(msg!=null){
			tvmsg.setText(msg);
		}else{
			tvmsg.setVisibility(View.GONE);
		}
		Button left = (Button)v.findViewById(R.id.dialog_btn_Left);
		if(LBtn!=null){
			left.setText(LBtn);
		}else{
			left.setVisibility(View.GONE);
		}
		Button right = (Button)v.findViewById(R.id.dialog_btn_Right);
		if(RBtn!=null){
			right.setText(RBtn);
		}else{
			right.setVisibility(View.GONE);
		}
		setContentView(v);//设置对话框的布局
		
		
		setProperty();
	}
	public void setLBtnListner(View.OnClickListener listener){
		if(mContentView!=null){
			Button left = (Button)mContentView.findViewById(R.id.dialog_btn_Left);
			left.setOnClickListener(listener);
		}
	}
	public void setRBtnListner(View.OnClickListener listener){
		if(mContentView!=null){
			Button right = (Button)mContentView.findViewById(R.id.dialog_btn_Right);
			right.setOnClickListener(listener);
		}
	}
	//要显示这个对话框，只要创建该类对象．然后调用该函数即可．
	public void setProperty(){
		window=getWindow();//　　　得到对话框的窗口�		
		WindowManager.LayoutParams wl = window.getAttributes();
		WindowManager m = m_context.getWindowManager(); 
		Display d = m.getDefaultDisplay();  //为获取屏幕宽、高  
//		wl.height = (int) (d.getHeight() * 0.4);   //高度设置为屏幕的0.6  
		wl.width = (int) (d.getWidth() * 0.95);    //宽度设置为屏幕的0.95  
		wl.alpha=1.0f;
		window.setAttributes(wl); 
	}
	public void setProperty(float height){
		window=getWindow();//　　　
		WindowManager.LayoutParams wl = window.getAttributes();
		WindowManager m = m_context.getWindowManager(); 
		Display d = m.getDefaultDisplay();  //为获取屏幕宽、高  
		wl.height = (int) (d.getHeight() * height);   //高度设置为屏幕的0.6  
		wl.width = (int) (d.getWidth() * 0.8);    //宽度设置为屏幕的0.95  
		wl.alpha=1.0f;
		window.setAttributes(wl); 
	}

}
